package com.epfresh.supplier.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.recyclerview.extensions.ListAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.MerChantInfo;
import com.epfresh.supplier.widget.CommonRecyclerAdapter;
import com.epfresh.supplier.widget.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class HomeTopPopAdapter extends BaseAdapter {
    private Context context;
    private List<MerChantInfo> merChantInfoList;

    public HomeTopPopAdapter(Context context, List<MerChantInfo> merChantInfoList) {
        this.context = context;
        this.merChantInfoList = merChantInfoList;
    }

    @Override
    public int getCount() {
        return merChantInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHoldr viewHoldr;
        if (convertView == null) {
            viewHoldr = new ViewHoldr();
            convertView = LayoutInflater.from(context).inflate(R.layout.index_top_pop, null);
            viewHoldr.popTv = convertView.findViewById(R.id.tv_name);
            viewHoldr.view = convertView.findViewById(R.id.view);
            convertView.setTag(viewHoldr);
        } else {
            viewHoldr = (ViewHoldr) convertView.getTag();
        }

        if (merChantInfoList.get(position).isSelect()) {
            Drawable drawable = viewHoldr.popTv.getContext().getResources().getDrawable(R.mipmap.icon_current_store);
            // 这一步必须要做,否则不会显示.
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            viewHoldr.popTv.setCompoundDrawables(drawable, null, null, null);
            viewHoldr.popTv.setTextColor(0xffff0000);
        } else {
            viewHoldr.popTv.setTextColor(0xff626262);
            viewHoldr.popTv.setCompoundDrawables(null, null, null, null);
        }
        viewHoldr.popTv.setText(merChantInfoList.get(position).getNamePlus());
        if (position == merChantInfoList.size() - 1) {
            viewHoldr.view.setVisibility(View.VISIBLE);
        } else {
            viewHoldr.view.setVisibility(View.GONE);
        }
        return convertView;
    }

    class ViewHoldr {
        private TextView popTv;
        private View view;
    }
}

